package org.lboot.llog.module;

import cn.hutool.core.lang.Validator;
import lombok.extern.slf4j.Slf4j;
import org.lboot.jpa.jpa.AbstractJpaCurdExt;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ApiLogServiceImpl extends AbstractJpaCurdExt<Long, ApiLogEntity, ApiLogEntityDAO> implements ApiLogService {

    private static List<String> modules = null;

    @Override
    public List<String> getDistinctModule() {
        return getDistinctModule(true);
    }
    @Override
    public List<String> getDistinctModule(Boolean useCache) {
        if (useCache){
            if (Validator.isNotEmpty(modules)){
                return modules;
            }else {
                List<String> result = dao.findDistinctModule();
                if (!result.isEmpty()){
                   modules = result;
                }
                return result;
            }
        }else {
            List<String> result = dao.findDistinctModule();
            if (!result.isEmpty()){
                modules = result;
            }
            return result;
        }
    }

    @Override
    public List<ApiLogEntity> getByTraceId(String traceId) {
        return dao.findByTraceId(traceId);
    }

    @Override
    public void resetModules() {
        modules = null;
    }
}
